package edu.gdkm.collection;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class StudentManagerByMap {
    Map<String,Student> studentMap= new HashMap<>();
    //定义一个初始化方法，读取文件score中的信息
    public void init() throws IOException {
        File file = new File(".\\doc\\score.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s;
        //读取一行数据，通过分隔符切出6段记录，分别放入Student这个实体类的对象中
        //最后将这个对象存入studentMap中去
        while ((s = br.readLine()) != null) {
            String[] records = s.split(",");
            Student student = new Student();
            student.setSname(records[0]);
            student.setSnumber(records[1]);
            student.setScore1(Double.parseDouble(records[2]));
            student.setScore2(Double.parseDouble(records[3]));
            student.setScore3(Double.parseDouble(records[4]));
            student.setScore4(Double.parseDouble(records[5]));
            studentMap.put(student.getSnumber(),student);//将对象存入到map中去
        }
    }
    /**
     * 1、设计方法，获取学生人数
     * */
    public int getStudentCount(){
        return studentMap.size();
    }
    /**
     * 2、设计方法，输出所有学生信息
     * */
    public void printStudentList(){
//        for (int i = 0; i < studentList.size(); i++) {
//            System.out.println(studentList.get(i));
//        }
        //使用Iterator遍历集合,Iterator是集合遍历的专用接口，效率较高
        Iterator iterator=studentMap.values().iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    /**
     * 3、设计方法，通过学号查询学生信息
     * */
    public Student findStudentBySnumber(String snumber){
        return  studentMap.get(snumber);
    }
}
